package com.mohe.shanpao.kafka;

import java.util.Arrays;
import java.util.Properties;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;


public class MyConsumer1 {

	private KafkaConsumer<String, String> consumer = null;
	
	public static void main(String[] args) {
		MyConsumer1 mc = new MyConsumer1();
		
		mc.init();
		
		mc.consumer.subscribe(Arrays.asList("ttt"));
		
		while(true)
		{
			ConsumerRecords<String, String> records = mc.consumer.poll(5);
			for(ConsumerRecord<String,String> record : records)
			{
				System.out.printf("MyConsumer1 offset = %d, key = %s, value = %s", record.offset(), record.key(), record.value());
			}
		}
	}
	
	public void init()
	{
		Properties props = new Properties();
		props.put("bootstrap.servers", "192.168.91.130:9092,192.168.91.131:9092,192.168.91.133:9092");
		props.put("group.id", "group1");
		
		props.put("enable.auto.commit", "true");
		props.put("auto.commit.interval.ms", "1000");
		props.put("session.timeout.ms", "30000");
		props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
		props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
		
		consumer = new KafkaConsumer<>(props);
	}
}
